//Global variables
var c1sel=false, c2sel=false, c3sel=false, c4sel=false;
var c1val, c2val, c3val, c4val, resvalue, tolerance;
//Functions
function dovtc()
{
	document.getElementById("ctt").checked=false;
}
function doctv()
{
	document.getElementById("ttc").checked=false;
}
function calculatectv()
{
	var digit1, digit2, power;
	switch(c1val)
	{
	case "c1r1":
		digit1=0;
		break;
	case "c1r2":
		digit1=1;
		break;
	case "c1r3":
		digit1=2;
		break;
	case "c1r4":
		digit1=3;
		break;
	case "c1r5":
		digit1=4;
		break;
	case "c1r6":
		digit1=5;
		break;
	case "c1r7":
		digit1=6;
		break;
	case "c1r8":
		digit1=7;
		break;
	case "c1r9":
		digit1=8;
		break;
	case "c1r10":
		digit1=9;
		break;
	}
	switch(c2val)
	{
	case "c2r1":
		digit2=0;
		break;
	case "c2r2":
		digit2=1;
		break;
	case "c2r3":
		digit2=2;
		break;
	case "c2r4":
		digit2=3;
		break;
	case "c2r5":
		digit2=4;
		break;
	case "c2r6":
		digit2=5;
		break;
	case "c2r7":
		digit2=6;
		break;
	case "c2r8":
		digit2=7;
		break;
	case "c2r9":
		digit2=8;
		break;
	case "c2r10":
		digit2=9;
		break;
	}
	switch(c3val)
	{
	case "c3r1":
		power=0;
		break;
	case "c3r2":
		power=1;
		break;
	case "c3r3":
		power=2;
		break;
	case "c3r4":
		power=3;
		break;
	case "c3r5":
		power=4;
		break;
	case "c3r6":
		power=5;
		break;
	case "c3r7":
		power=6;
		break;
	case "c3r8":
		power=7;
		break;
	case "c3r9":
		power=8;
		break;
	case "c3r10":
		power=9;
		break;
	}
	var prefix="Nominal value is ";
	var x=digit1.toString()+digit2.toString();
	x=x*1;
	var z=Math.pow(10, power);
	x=x*z; //Resistance value in Ohms
	resvalue=x;
	if(x<1000)
	{
	//Value is in Ohms
		document.getElementById("num-value").innerHTML=prefix+x+" Ohm.";
	}
	else if(x>=1000 && x<1000000)
	{
	//Value is in KOhms
		x=x/1000;
		document.getElementById("num-value").innerHTML=prefix+x+" KOhm.";
	}
	else
	{
	//Value is MOhms
		x=x/1000000;
		document.getElementById("num-value").innerHTML=prefix+x+" MOhm.";
	}
	if(c1sel && c2sel && c3sel && c4sel)
	{
		calcminmax(resvalue, tolerance);
	}
}
function calculatetolerance(elementid)
{
	var prefix="<abbr id=\"tolabbr\" title=\"\">Tolerance is ";
	var postfix=" %.</abbr>";
	
	switch(elementid)
	{
	case "c4r1":
		document.getElementById("numtol").innerHTML=prefix+"&#177;20"+postfix;
		tolerance=20;
		break;
	case "c4r2":
		document.getElementById("numtol").innerHTML=prefix+"&#177;10"+postfix;
		tolerance=10;
		break;
	case "c4r3":
		document.getElementById("numtol").innerHTML=prefix+"&#177;5"+postfix;
		tolerance=5;
		break;
	case "c4r4":
		document.getElementById("numtol").innerHTML=prefix+"&#177;2"+postfix;
		tolerance=2;
		break;
	case "c4r5":
		document.getElementById("numtol").innerHTML=prefix+"&#177;1"+postfix;
		tolerance=1;
		break;
	case "c4r6":
		document.getElementById("numtol").innerHTML=prefix+"&#177;0.5"+postfix;
		tolerance=0.5;
		break;
	case "c4r7":
		document.getElementById("numtol").innerHTML=prefix+"&#177;0.25"+postfix;
		tolerance=0.25;
		break;
	case "c4r8":
		document.getElementById("numtol").innerHTML=prefix+"&#177;0.1"+postfix;
		tolerance=0.1;
		break;
	case "c4r9":
		document.getElementById("numtol").innerHTML=prefix+"&#177;0.05"+postfix;
		tolerance=0.05;
		break;
	}
	if(c1sel && c2sel && c3sel)
	{
		calcminmax(resvalue, tolerance);
	}
}
function calcminmax(value, tolerance)
{
	var dotindex, minvalue, maxvalue;
	minvalue=value*(1-(tolerance/100));
	minvalue=minvalue.toString();
	dotindex = minvalue.indexOf('.');
	if(dotindex > 0)
	{
		minvalue=minvalue.substring(0, dotindex+4);
	}
	maxvalue=value*(1+(tolerance/100));
	maxvalue=maxvalue.toString();
	dotindex = maxvalue.indexOf('.');
	if(dotindex > 0)
	{
		maxvalue=maxvalue.substring(0, dotindex+4);
	}	
	document.getElementById("tolabbr").title="Real value is between "+minvalue+" and "+maxvalue+" Ohm.";
}
function selected(elementid)
{
	var tmpc1=false, tmpc2=false, tmpc3=false, tmpc4=false;
	var workc;

	var patt=new RegExp("c1");
	tmpc1=patt.test(elementid);

	var patt=new RegExp("c2");
	tmpc2=patt.test(elementid);

	var patt=new RegExp("c3");
	tmpc3=patt.test(elementid);
	
	var patt=new RegExp("c4");
	tmpc4=patt.test(elementid);
	
	workc=(tmpc1==true)?"c1":false;
	workc=(tmpc2==true)?"c2":workc;
	workc=(tmpc3==true)?"c3":workc;
	workc=(tmpc4==true)?"c4":workc;
		
	if(workc=="c1")
	{
		if(c1sel==false)
		{
			c1sel=true;
			c1val=elementid;
			document.getElementById(elementid).style.border="3px solid #0033FF";
		}
		else
		{
			c1sel=true;
			document.getElementById(c1val).style.border="none";
			c1val=elementid;
			document.getElementById(c1val).style.border="3px solid #0033FF";
		}
	}
	else if(workc=="c2")
	{
		if(c2sel==false)
		{
			c2sel=true;
			c2val=elementid;
			document.getElementById(elementid).style.border="3px solid #0033FF";
		}
		else
		{
			c2sel=true;
			document.getElementById(c2val).style.border="none";
			c2val=elementid;
			document.getElementById(c2val).style.border="3px solid #0033FF";
		}
	}	
	else if(workc=="c3")
	{
		if(c3sel==false)
		{
			c3sel=true;
			c3val=elementid;
			document.getElementById(elementid).style.border="3px solid #0033FF";
		}
		else
		{
			c3sel=true;
			document.getElementById(c3val).style.border="none";
			c3val=elementid;
			document.getElementById(c3val).style.border="3px solid #0033FF";
		}
	}
	else if(workc=="c4")
	{
		if(c4sel==false)
		{
			c4sel=true;
			c4val=elementid;
			document.getElementById(elementid).style.border="3px solid #0033FF";
		}
		else
		{
			c4sel=true;
			document.getElementById(c4val).style.border="none";
			c4val=elementid;
			document.getElementById(c4val).style.border="3px solid #0033FF";
		}
		calculatetolerance(elementid);	
	}
	if(c1sel && c2sel && c3sel)
	{
		calculatectv();
	}
}
function normalizevalue(value, dimension)
{
	
	var multip;
	if(dimension == 'O') {
		multip = 1;
	}
	else if (dimension == 'K') {
		multip = 1000;
	}
	else if (dimension == 'M') {
		multip = 1000000;
	}
	return value*multip;
}
function calculatevtc(value, dimension)
{
	
	document.getElementById('vtc-answ').innerHTML = "XXX";
	document.getElementById('vtc-exact').innerHTML = "EEE";
	document.getElementById('vtc-lower').innerHTML = "YYY";
	document.getElementById('vtc-higher').innerHTML = "ZZZ";

	valueohm=normalizevalue(value, dimension);
	
	var E24 = new Array();
	E24[0]=1.0; E24[1]=1.1; E24[2]=1.2; E24[3]=1.3; E24[4]=1.5; E24[5]=1.6; E24[6]=1.8; E24[7]=2.0;
	E24[8]=2.2; E24[9]=2.4; E24[10]=2.7; E24[11]=3.0; E24[12]=3.3; E24[13]=3.6; E24[14]=3.9; E24[15]=4.3;
	E24[16]=4.7; E24[17]=5.1; E24[18]=5.6; E24[19]=6.2; E24[20]=6.8; E24[21]=7.5; E24[22]=8.2; E24[23]=9.1;

	if (valueohm < 0.1) {
	//TODO: Alert error
	}
	
	var exponential = valueohm.toExponential(1);
	var exponent = exponential.substr(0,3);
	var fraction = exponential.substr(exponential.indexOf('e')+1, exponential.length-exponential.indexOf('e')+1);
	var adjust = Math.pow(10, fraction);	
	
	var adjustedohmvalue = valueohm / adjust;

	document.getElementById('vtc-answ').innerHTML = adjustedohmvalue;
	
	var diff;
	var i=23;
	do {
		diff = E24[i] - adjustedohmvalue;
		if (diff == 0) {
			var result = E24[i]*adjust;			
			document.getElementById('vtc-exact').innerHTML = result;

			/* Calculate lower value */			
			if (i > 0) {				
				var lowerindex = i-1;				
				document.getElementById('vtc-lower').innerHTML = E24[lowerindex]*adjust;
			}
			else if (i == 0 && fraction > -1) {				
				document.getElementById('vtc-lower').innerHTML = E24[23]*Math.pow(10, fraction-1);
			}
			else {
				document.getElementById('vtc-lower').innerHTML = "No lower value!";			
			}

			/* Calculate higher value */
			if (i < 23 && fraction <= 6) {				
				var higherindex = i+1;				
				document.getElementById('vtc-higher').innerHTML = E24[higherindex]*adjust;
			}
			else if (i == 23 && fraction <= 6) {				
				document.getElementById('vtc-higher').innerHTML = E24[0]*Math.pow(10, Number(fraction)+1);
			}
			else {
				document.getElementById('vtc-higher').innerHTML = "No higher value!";			
			}
			break;		
		}

	} while(i--)

/*
	var E192 = new Array();
	E192[0]=100; E192[1]=101; E192[2]=102; E192[3]=104; E192[4]=105; E192[5]=106; E192[6]=107; 
	E192[7]=109; E192[8]=110; E192[9]=111; E192[10]=113; E192[11]=114; E192[12]=115; E192[13]=117;
	E192[14]=118; E192[15]=120; E192[16]=121; E192[17]=123; E192[18]=124; E192[19]=126; E192[20]=127;
	E192[21]=129; E192[22]=130; E192[23]=132; E192[24]=133; E192[25]=135; E192[26]=137; E192[27]=138;
	E192[28]=140; E192[29]=142; E192[30]=143; E192[31]=145; E192[32]=147; E192[33]=149; E192[34]=150;
	E192[35]=152; E192[36]=154; E192[37]=156; E192[38]=158; E192[39]=160; E192[40]=162; E192[41]=164;
	E192[42]=165; E192[43]=167; E192[44]=169; E192[45]=172; E192[46]=174; E192[47]=176; E192[48]=178;
	E192[49]=180; E192[50]=182; E192[51]=184; E192[52]=187; E192[53]=189; E192[54]=191; E192[55]=193;
	E192[56]=196; E192[57]=198; E192[58]=200; E192[59]=203; E192[60]=205; E192[61]=208; E192[62]=210;
	E192[63]=213; E192[64]=215; E192[65]=218; E192[66]=221; E192[67]=223; E192[68]=226; E192[69]=229;
	E192[70]=232; E192[71]=234; E192[72]=237; E192[73]=240; E192[74]=243; E192[75]=246; E192[76]=249;
	E192[77]=252; E192[78]=255; E192[79]=258; E192[80]=261; E192[81]=264; E192[82]=267; E192[83]=271;
	E192[84]=274; E192[85]=277; E192[86]=280; E192[87]=284; E192[88]=287; E192[89]=291; E192[90]=294;
	E192[91]=298; E192[92]=301; E192[93]=305; E192[94]=309; E192[95]=312; E192[96]=316; E192[97]=320;
	E192[98]=324; E192[99]=328; E192[100]=332; E192[101]=336; E192[102]=340; E192[103]=344; E192[104]=348;
	E192[105]=352; E192[106]=357; E192[107]=361; E192[108]=365; E192[109]=370; E192[110]=374;
	E192[111]=379; E192[112]=383; E192[113]=388; E192[114]=392; E192[115]=397; E192[116]=402;
	E192[117]=407; E192[118]=412; E192[119]=417; E192[120]=422; E192[121]=427; E192[122]=432;
	E192[123]=437; E192[124]=442; E192[125]=448; E192[126]=453; E192[127]=459; E192[128]=464;
	E192[129]=470; E192[130]=475; E192[131]=481; E192[132]=487; E192[133]=493; E192[134]=499;
	E192[135]=505; E192[136]=511; E192[137]=517; E192[138]=523; E192[139]=530; E192[140]=536;
	E192[141]=542; E192[142]=549; E192[143]=556; E192[144]=562; E192[145]=569; E192[146]=576;
	E192[147]=583; E192[148]=590; E192[149]=597; E192[150]=604; E192[151]=612; E192[152]=619;
	E192[153]=626; E192[154]=634; E192[155]=642; E192[156]=649; E192[157]=657; E192[158]=665;
	E192[159]=673; E192[160]=681; E192[161]=690; E192[162]=698; E192[163]=706; E192[164]=715;
	E192[165]=723; E192[166]=732; E192[167]=741; E192[168]=750; E192[169]=759; E192[170]=768;
	E192[171]=777; E192[172]=787; E192[173]=796; E192[174]=806; E192[175]=816; E192[176]=825;
	E192[177]=835; E192[178]=845; E192[179]=856; E192[180]=866; E192[181]=876; E192[182]=887;
	E192[183]=898; E192[184]=909; E192[185]=920; E192[186]=931; E192[187]=942; E192[188]=953;
	E192[189]=965; E192[190]=976; E192[191]=988;
*/
}
